#ifndef MCPWM_H_
#define MCPWM_H_

#include <tT.h>
#include <env.h>

//Pull down on DISABLE-pin for MOSFET Drivers for 0.1 micro sec
/* Drive Current Reset pin LOW UM p.126*/
/* Set ports to output when in general mode p.123 */
#define CUR_RESET()\
	PINCON->PINMODE3 &=	~(3<<8);\
	PINCON->PINMODE3 |=	(3<<10);\
	GPIO1->FIOCLR = (1<<20);\
	GPIO1->FIODIR |=  (1<<20);\
	TT_AFTER_BEFORE(10000,10000, &mcpwm, mcpwm_reset_clear, TT_ARGS_NONE)

#define MC_DEADTIME 10 		//Deadtime default = 10 clock cykles
#define MC_OFF -1
#define MC_LOW 0
#define MC_HIGH 1
	
typedef struct mcpwm_t
{
	tt_object_t super;
	
	uint32_t status;	// 1 = enable 0 = disable.
	int32_t duty;		//duty cykel for pmw. -2500 - 2500
	uint32_t dt;		//deadtime
	int32_t speed;		//rev/min
	uint32_t poles;
	uint32_t counter;
	uint32_t per[8];
	uint32_t prev[4];
	
	
} mcpwm_t;

#define mcpwm() {tt_object(), 0, 0, 0, 2}

extern mcpwm_t mcpwm;
extern tt_object_t *token;

env_result_t mcpwm_init(mcpwm_t *self, void *none);
env_result_t mcpwm_enable(mcpwm_t *self, void *none);
env_result_t mcpwm_disable(mcpwm_t *self, void *none);
env_result_t mcpwm_set_duty(mcpwm_t *self, int32_t *val);
env_result_t mcpwm_set_dt(mcpwm_t *self, uint32_t *val);
env_result_t mcpwm_reset_clear(mcpwm_t *self, void *none);
env_result_t mcpwm_decode(mcpwm_t *self, uint32_t data[2]);
void mcpwm_capture( uint32_t arg);
void d_delay(uint32_t count);
void u_leg(int32_t high);
void v_leg(int32_t high);
void w_leg(int32_t high);

#endif
